package top.lyjwn.ajui.widget.recycler.base;

import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import top.lyjwn.ajui.widget.list.ListView;
import top.lyjwn.ajui.widget.recycler.base.BaseViewHolder;

public abstract class RecyclerBaseListAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {


    protected List<T> TList = new ArrayList<>();

    @NonNull
    @Override
    public abstract BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType);

    @Override
    public abstract void onBindViewHolder(@NonNull BaseViewHolder holder, int position);


    @Override
    public int getItemCount() {
        return TList.size();
    }

    //  添加数据
    public void addData(T t, int position) {
//      在list中添加数据，并通知条目加入一条
        TList.add(t);
        //添加动画
        notifyItemInserted(position);
    }

    public void add(List<T> t) {
        final int itemCount = getItemCount();
        for (int i = 0; i < t.size(); i++) {
            addData(t.get(i), itemCount + i);
        }
    }

    //  删除数据
    public void removeData(int position) {
        TList.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    //  删除数据
    public void removeAllChilds() {
        TList.clear();
        notifyDataSetChanged();
    }

    public void refresh(List<T> tList) {
//        removeAllChilds();
//        add(t);
        TList.clear();
        TList=tList;
        notifyDataSetChanged();
    }

    public void initBroccoliList(int n) {
        for (int i = 0; i < n; i++) {
            TList.add(null);
        }
    }

}
